package phone.example.com.a36kr.fragment.registerfragment;

import android.animation.TypeEvaluator;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AlertDialog;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;
import java.util.concurrent.ExecutionException;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.UpdateListener;
import phone.example.com.a36kr.R;
import phone.example.com.a36kr.activity.RegisterActivity;
import phone.example.com.a36kr.bean.UserBean;
import phone.example.com.a36kr.other.mybmob.MyBmobUser;
import phone.example.com.a36kr.tools.BaseFragment;
import phone.example.com.a36kr.tools.MyApp;

/**
 * Created by dllo on 16/8/26.
 */
public class RegisterLoginFragment extends BaseFragment {

    private EditText loginNameEt;
    private String loginPassword;
    private String loginName;
    private EditText loginPasswordEt;

    @Override
    protected int setLayout() {
        return R.layout.fragment_login_register;
    }

    @Override
    protected void initView() {
        //登录按钮
        final Button loginBtn = bindView(R.id.register_login_btn);
        TextView forgetPassTv = bindView(R.id.register_login_password_forget);
        forgetPassTv.setOnClickListener(new View.OnClickListener() {
            // dialog
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "改不了哦", Toast.LENGTH_SHORT).show();
            }
        });



        //登录用户名
        loginNameEt = bindView(R.id.register_login_name_et);
        //登录密码
        loginPasswordEt = bindView(R.id.register_login_password_et);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginName = loginNameEt.getText().toString();
                loginPassword = loginPasswordEt.getText().toString();
                MyBmobUser.loginByAccount(loginName, loginPassword, new LogInListener<MyBmobUser>() {
                    @Override
                    public void done(MyBmobUser myBmobUser, BmobException e) {


                        if (myBmobUser != null) {
                            /**
                             * fragment发送广播
                             */
                            Intent intent = new Intent();
                            intent.setAction("com.36kr.android.Login");
                            Log.d("登录成功", "我发送了广播");
                            LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);

                            /**
                             * 登录
                             */
                            Toast.makeText(mContext, "登录成功", Toast.LENGTH_SHORT).show();
                            getActivity().finish();

                        } else {
                            Toast.makeText(mContext, "登录失败", Toast.LENGTH_SHORT).show();
                            Log.d("RegisterLoginFragment", "e:" + e);
                        }
                    }
                });
            }
        });

        CheckBox checkBox = bindView(R.id.register_password_cb);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    //选中,显示密码
                    loginPasswordEt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else {
                    loginPasswordEt.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
    }

    @Override
    protected void initData() {


    }
}
